{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import backtrader as bt\n",
    "import datetime\n",
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tusharefeed import TushareData\n",
    "feed = TushareData(\n",
    "    dataname='159915',\n",
    "    fromdate=datetime.date(2020,1,1),\n",
    "    todate=datetime.date(2020,6,1), \n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "class BRindicator(bt.Indicator):\n",
    "    lines = ('br',)\n",
    "    params = (('period', 26),)\n",
    "\n",
    "    plotlines = dict(br=dict(ls='--', color='blue'))\n",
    "\n",
    "    def __init__(self):\n",
    "        self.addminperiod(self.params.period+1)\n",
    "\n",
    "    def next(self):\n",
    "        th = np.array(self.data.high.get(ago=0, size=self.p.period))\n",
    "        tl = np.array(self.data.low.get(ago=0, size=self.p.period))\n",
    "        yc = np.array(self.data.close.get(ago=-1, size=self.p.period))\n",
    "        self.lines.br[0] = np.sum(th-yc)/np.sum(yc-tl) *100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a Stratey\n",
    "class EmptyStrategy(bt.Strategy):\n",
    "    '''\n",
    "    Empty strategy is used to study indicators\n",
    "    '''\n",
    "    params = (\n",
    "        ('none', 0),\n",
    "    )\n",
    "        \n",
    "    def __init__(self):\n",
    "        self.br = BRindicator(self.data0, period=10)\n",
    "        pass\n",
    "        \n",
    "    def next(self):\n",
    "        print('BR:{}'.format(self.br[0]))\n",
    "        pass \n",
    "    \n",
    "    def stop(self):\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cerebro = bt.Cerebro(oldtrades=False, stdstats=False)\n",
    "cerebro.adddata(feed, name='cy_daily')\n",
    "#cerebro.resampledata(feed, name='cy_weekly', timeframe=bt.TimeFrame.Weeks)\n",
    "\n",
    "\n",
    "cerebro.addstrategy(EmptyStrategy)\n",
    "\n",
    "# exp indicators\n",
    "#cerebro.addindicator(bt.indicators.DirectionalMovementIndex)\n",
    "#cerebro.addindicator(BRindicator, period=10)\n",
    "\n",
    "# init cash\n",
    "cerebro.broker.setcash(10000.0)\n",
    "\n",
    "print('Starting Running')\n",
    "\n",
    "result = cerebro.run()\n",
    "\n",
    "print('Finish Running')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "params = dict(\n",
    "    style='candle',\n",
    "    barup='#FF0033',\n",
    "    bardown='#32CD32',\n",
    "    volup='#F66269',\n",
    "    voldown='#43A047',\n",
    ")\n",
    "cerebro.plot(\n",
    "    iplot=False, \n",
    "    **params\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
